package com.itHsuSE.demo12;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.StringTokenizer;

/**
 * @author Hsu琛君珩
 * @ClassName:MyScanner
 * @date 2024-01-29
 * @apiNote
 * @Version: v1.0
 */

public class MyScanner {
    BufferedReader br;
    StringTokenizer st;

    public MyScanner(){
        try {
            final String USERDIR=System.getProperty("user.dir");
            String cname= MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner","");
            cname=cname.lastIndexOf('.')>0?cname.substring(cname.lastIndexOf('.')+1):cname;
            final File fin=new File(USERDIR+"/io/c"+cname.substring(1,5)+"/"+cname+".in");
            br=new BufferedReader(new InputStreamReader(fin.exists()?new FileInputStream(fin):System.in));
        }catch (Exception e){
            br=new BufferedReader(new InputStreamReader(System.in));
        }
    }

    public String next(){
        try {
            while(st==null||!st.hasMoreElements()){
                st=new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public int nextInt(){
        return Integer.parseInt(next());
    }

    public long nextLong(){
        return Long.parseLong(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public String nextLine() {
        try {
            return br.readLine();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
